⚡️ Speed up method ImportAnalyzer.visit_Call by 31% in PR #867 (inspect-signature-issue)
#878
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #867
If you approve this dependent PR, these changes will be merged into the original PR branch
inspect-signature-issue.📄 31% (0.31x) speedup for
ImportAnalyzer.visit_Callincodeflash/discovery/discover_unit_tests.py⏱️ Runtime :
13.0 milliseconds→9.94 milliseconds(best of41runs)📝 Explanation and details
The optimization replaces the standard
ast.NodeVisitor.generic_visitcall with a custom_fast_generic_visitmethod that inlines the AST traversal logic, eliminating method resolution overhead and adding more aggressive early-exit checks.Key Performance Improvements:
Eliminated Method Resolution Overhead: The original code called
ast.NodeVisitor.generic_visit(self, node)which incurs method lookup and dispatch costs. The optimized version inlines this logic directly, avoiding the base class method call entirely.More Frequent Early Exit Checks: The new
_fast_generic_visitchecksself.found_any_target_functionat multiple points during traversal (before processing lists and individual AST nodes), allowing faster short-circuiting when a target function is found.Optimized Attribute Access: The optimization uses direct
getattrcalls and caches method lookups (getattr(self, 'visit_' + item.__class__.__name__, None)) to reduce repeated attribute resolution.Performance Impact by Test Case:
The line profiler shows the optimization reduces time spent in
generic_visitfrom 144.2ms to 107.9ms (25% improvement), with the overallvisit_Callmethod improving from 287.5ms to 210.3ms. This optimization is particularly valuable for AST analysis tools that process large codebases, as the traversal overhead reduction scales with the size and complexity of the analyzed code.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr867-2025-11-05T08.40.37and push.